#!/usr/bin/python
# OpenDyn - Open Source Dyanmic DNS system
#
# Copyright 2009 by Koen Sadza <koen@koensadza.nl> http://koensadza.nl
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import MySQLdb
import sys
import hashlib

if len(sys.argv) < 5:
    print "Usage: %s <mysql_user> <mysql_password> <mysql_dbname> <adminpassword>" %(sys.argv[0],)
    sys.exit(1)

DB_USER,DB_PASS,DB_NAME,ADMPASS = sys.argv[1:]

try:
    db = MySQLdb.connect(user=DB_USER,passwd=DB_PASS,db=DB_NAME) #Connect to the database
except MySQLdb.Error, e:
    print e[1]
    sys.exit(1)

cursor = db.cursor()
m = hashlib.sha256(ADMPASS)

cursor.execute("CREATE TABLE `hosts` (   `id` mediumint(8) unsigned NOT NULL auto_increment,   `hostname` text,   `password` text,   `last_ip` int(10) unsigned default NULL,   PRIMARY KEY  (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;")
cursor.execute("INSERT INTO hosts (hostname,password) VALUES ('admin','%s');" %(m.hexdigest(),))
cursor.close()
db.commit()
db.close()

print "Successfully created hosts table and admin account"
print "Try to login with hostname 'admin' and the password you submitted to add hosts"
